package com.nova;

import java.util.ArrayList;
import android.app.Activity;

public class ActivityManager
{

	private static ArrayList<Activity> mActivities;

	public static Activity getActivityByClass(Class<?> cl)
	{
		ArrayList<Activity> temp = getActivitysByClass(cl);
		return temp.size() != 0 ? (Activity) temp.get(temp.size() - 1) : null;
	}

	public static ArrayList<Activity> getActivitysByClass(Class<?> cl)
	{
		ArrayList<Activity> temp = new ArrayList<Activity>();
		for (Activity ac : mActivities)
		{
			if (ac.getClass().equals(cl))
				temp.add(ac);
		}
		return temp;
	}

	public static void add(Activity activity)
	{
		if (mActivities == null)
			mActivities = new ArrayList<Activity>();
		if (!mActivities.contains(activity))
			mActivities.add(activity);
	}

	public static void remove(Activity activity)
	{
		if (mActivities != null)
			mActivities.remove(activity);
	}

	public static Activity getLastActivity()
	{
		int size = mActivities == null ? 0 : mActivities.size();
		if (size > 0)
		{
			return (Activity) mActivities.get(size - 1);
		}
		return null;
	}

	public static void finishAll()
	{
		if (mActivities != null)
		{
			ArrayList<Activity> tempList = new ArrayList<Activity>();
			tempList.addAll(mActivities);
			for (Activity activity : tempList)
				activity.finish();
			mActivities.clear();
		}
	}
}
